package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.MsgGetHtmlDTO;
import com.xbongbong.paas.pojo.dto.MsgGetReceiptStatusDTO;
import com.xbongbong.paas.pojo.dto.MsgHaveReadDTO;
import com.xbongbong.paas.pojo.dto.MsgReceiptSaveDTO;
import com.xbongbong.paas.pojo.dto.MsgSurveyHasAnswerDTO;
import com.xbongbong.paas.pojo.dto.MsgSurveySaveDTO;
import com.xbongbong.paas.pojo.vo.MsgGetHtmlVO;
import com.xbongbong.paas.pojo.vo.MsgGetReceiptStatusVO;
import com.xbongbong.paas.pojo.vo.MsgHaveReadVO;
import com.xbongbong.paas.pojo.vo.MsgReceiptSaveVO;
import com.xbongbong.paas.pojo.vo.MsgSurveyHasAnswerVO;
import com.xbongbong.paas.pojo.vo.MsgSurveySaveVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.AdminMsgService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * admin中的消息操作
 * 创建时间 2019/4/15 8:26 PM
 * 修改时间 2019/4/15 8:26 PM
 * @author chy
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/msg")
public class AdminMsgController {


    @Resource
    private AdminMsgService adminMsgService;

    /**
     * 获取页面
     * @param msgGetHtmlDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getHtml", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getHtml(@RequestBody @Valid MsgGetHtmlDTO msgGetHtmlDTO, BindingResult br) {
        XbbResponse<MsgGetHtmlVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgGetHtmlVO msgGetHtmlVO = adminMsgService.getHtml(msgGetHtmlDTO);
                response = new XbbResponse<>(msgGetHtmlVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 标记已读
     * @param msgHaveReadDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/haveRead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String haveRead(@RequestBody @Valid MsgHaveReadDTO msgHaveReadDTO, BindingResult br) {
        XbbResponse<MsgHaveReadVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgHaveReadVO msgHaveReadVO = adminMsgService.haveRead(msgHaveReadDTO);
                response = new XbbResponse<>(msgHaveReadVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回执保存
     * @param msgReceiptSaveDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/receiptSave", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String receiptSave(@RequestBody @Valid MsgReceiptSaveDTO msgReceiptSaveDTO, BindingResult br) {
        XbbResponse<MsgReceiptSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgReceiptSaveVO msgReceiptSaveVO = adminMsgService.receiptSave(msgReceiptSaveDTO);
                response = new XbbResponse<>(msgReceiptSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取回执状态
     * @param msgGetReceiptStatusDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getReceiptStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getReceiptStatus(@RequestBody @Valid MsgGetReceiptStatusDTO msgGetReceiptStatusDTO, BindingResult br) {
        XbbResponse<MsgGetReceiptStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgGetReceiptStatusVO msgGetReceiptStatusVO = adminMsgService.getReceiptStatus(msgGetReceiptStatusDTO);
                response = new XbbResponse<>(msgGetReceiptStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 判断是否已经答过此问卷
     * @param msgSurveyHasAnswerDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/survey/hasAnswer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hasAnswer(@RequestBody @Valid MsgSurveyHasAnswerDTO msgSurveyHasAnswerDTO, BindingResult br) {
        XbbResponse<MsgSurveyHasAnswerVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgSurveyHasAnswerVO msgSurveyHasAnswerVO = adminMsgService.hasAnswer(msgSurveyHasAnswerDTO);
                response = new XbbResponse<>(msgSurveyHasAnswerVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 用户保存问卷
     * @param msgSurveySaveDTO
     * @param br
     * @return
     * 创建时间 2019/4/16 11:21 AM
     * 修改时间 2019/4/16 11:21 AM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/survey/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String surveySave(@RequestBody @Valid MsgSurveySaveDTO msgSurveySaveDTO, BindingResult br) {
        XbbResponse<MsgSurveySaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MsgSurveySaveVO msgSurveySaveVO = adminMsgService.surveySave(msgSurveySaveDTO);
                response = new XbbResponse<>(msgSurveySaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
